package design.manage;
import java.io.*;
import java.util.*;

import design.book.*;
public class BookManage {
	private static BookManage bookmanage= null;
	private Book b = null;
	private List<Book> booklist = new ArrayList<Book>();
	private FileOutputStream fos = null;
	private BufferedReader br = null;
	private File f = new File("1.txt");
	private String line = "";
	private String[] temp;
	private Scanner scan = new Scanner(System.in);
	//初始化需要的变量
	
	
	private BookManage(){  }
	public static BookManage getInstance(){ //单例模式
		if(bookmanage == null){
			bookmanage = new BookManage();
		}
		return bookmanage;
	}
	
	
	
	public void add(Book b){  //添加图书
		if(b == null)
			System.out.println("添加图书失败，请重新操作");
		else{
			booklist.add(b);
			System.out.println("添加成功");
		}
	}
	
	
	public void searchOne(String attribute){
		for(int i=0;i<booklist.size();i++){
			if(booklist.get(i).getBid().equalsIgnoreCase(attribute)){
				System.out.println(booklist.get(i).readString());
				return;
			}
		
		}
		System.out.println("您查找的图书不存在，请重新操作");
	}
	
	public void delete(String attribute){ //删除图书
		for(int i=0;i<booklist.size();i++){
			if(booklist.get(i).getBid().equalsIgnoreCase(attribute)){
				System.out.println("已将图书号为"+booklist.get(i).getBid()+"的图书移除,请重新查看");
				booklist.remove(i);
				break;
			}
		}
	}
	
	public void sortByPrice(String attribute){ //按照价格给图书排序
		//排序
		Collections.sort(booklist, new Comparator<Book>(){
			public int compare(Book b1, Book b2) {
				return b1.getBprice().compareTo(b2.getBprice());
			}
		});
			
		for(int i=0;i<booklist.size();i++){
			System.out.println(booklist.get(i).readString());
		}
		System.out.println("排序成功");	
	}
	
	public void searchAll(String attribute){ //查看所有图书
		for(int i=0;i<booklist.size();i++){
			System.out.println(booklist.get(i).readString());
		}
	}
	
	public void modify(String attribute){ //修改图书
		for(int i=0;i<booklist.size();i++){
			if(booklist.get(i).getBid().equalsIgnoreCase(attribute)){
				System.out.print("请输入要修改的类型(包括bid，bname,bauthor，btype，bprice，bamount，bpubdate等):");
				String type = scan.nextLine();
				System.out.print("请输入要修改的新值:");
				String newvalue = scan.nextLine();
				switch(type){
				case "bid": booklist.get(i).setBid(newvalue); break;
				case "bname": booklist.get(i).setBname(newvalue); break;
				case "bauthor": booklist.get(i).setBauthor(newvalue); break;
				case "btype": booklist.get(i).setBtype(newvalue); break;
				case "bprice": booklist.get(i).setBprice(Integer.parseInt(newvalue));break;
				case "bamount": booklist.get(i).setBamount(Integer.parseInt(newvalue)); break;
				case "bpubdate": booklist.get(i).setBpubdate(newvalue); break;
				default: System.out.println("图书没有该样属性，请重新操作"); break; 
				}
				System.out.println("修改成功，请重新查看");
				return;
			}
		}
		System.out.println("您要修改的图书不存在，请重新操作");
	}
	
	public void ReadFile(){ //读取文件
		try{
			booklist.clear();
			if(!f.exists())
				f.createNewFile();
			br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
			while((line=br.readLine())!=null){
				temp = line.split(",");
				b = new Book(temp[1],temp[3],temp[5],temp[7],Integer.parseInt(temp[9]),Integer.parseInt(temp[11]),temp[13]);
				booklist.add(b);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void WriteToFile(){  //写入文件
	try{
		fos = new FileOutputStream(f);
		for(int i=0;i<booklist.size();i++){
			fos.write(booklist.get(i).toString().getBytes());
			fos.write("\r\n".getBytes());
		}
		fos.flush();
		booklist.clear();
		System.out.println("写入文件成功");
	}
	catch(Exception e){
		e.printStackTrace();
	}
	finally{
		
		if(fos!=null)
			try {
				fos.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		if(br!=null)
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
	}
}
